package com.af.security.mgt.user;

import com.af.system.api.result.AfResult;
import com.af.system.api.result.AfResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;

@Controller
@RequestMapping("security/mgt/user")
public class UserController {
    private final String urlPath = "security/mgt/user/";

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private UserService userService;

    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping("list")
    public String list() {
        return urlPath + "list";
    }

    @PreAuthorize("hasAuthority('user:create')")
    @GetMapping("create")
    public String create(HttpServletRequest request) {
        return urlPath + "create";
    }


    @PreAuthorize("hasAuthority('user:update')")
    @GetMapping("update")
    public String update(HttpServletRequest request) {
        return urlPath + "update";
    }

    @PreAuthorize("hasAuthority('user:delete')")
    @GetMapping("delete")
    public String delete(HttpServletRequest request) {
        return urlPath + "delete";
    }

    @PreAuthorize("hasAuthority('user:view')")
    @GetMapping("view")
    public String view(HttpServletRequest request) {
        return urlPath + "view";
    }


    @GetMapping("list")
    @ResponseBody
    public AfResult getList(int offset, int limit, String search) {
        Page<User> pageInfo = this.userService.findByUsername(search, offset, limit);
        return AfResultUtils.success(pageInfo);
    }

//===================================保存/修改/删除方法=======================================
/*
    @RequestMapping("saveUI")
    public String saveUI(Integer id, HttpServletRequest request) {
        if (id != null) {
            User user = this.userService.getById(id);
            if (user != null) {
                request.setAttribute("User", user);
            }
            System.out.println(user.toString());
        }
        return urlPath + "User/saveUI";
    }


//    @RequiresPermissions(value = {"User:add", "User:update"})
    @RequestMapping("user/save")
    public String add(User user) {
        if (user.getId() != null) {
            user.setUpdateTime(new Date());
            userService.update(user);
        } else {
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            try {
                user.setPassword("123456");
//                User.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes("UTF-8")));
//            } catch (UnsupportedEncodingException e) {
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.userService.save(user);
        }
        return "redirect:/user/listUI";
    }

//    @RequiresPermissions("User:delete")
    @RequestMapping("delete/{ids}")
    @ResponseBody
    public Result delete(@PathVariable("ids") String ids) {
        String[] idsStr = ids.split(",");
        this.userService.deleteByIds(idsStr);
        return ResultUtils.success();
    }

//    @RequiresPermissions("User:setRole")
    @RequestMapping("setRole")
    @ResponseBody
    public Result setRole(int userId, String roleIds) {
        this.userService.saveUserRole(userId, roleIds);
        return ResultUtils.success();
    }
*/

}
